// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.audio.parameters;

import zombie.audio.FMODGlobalParameter;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.math.PZMath;
import zombie.iso.BuildingDef;
import zombie.iso.IsoMetaCell;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoWorld;
import zombie.iso.RoomDef;

public final class ParameterRoomType extends FMODGlobalParameter {
    static ParameterRoomType instance;
    static ParameterRoomType.RoomType roomType = null;

    public ParameterRoomType() {
        super("RoomType");
        instance = this;
    }

    @Override
    public float calculateCurrentValue() {
        return (float)this.getRoomType().label;
    }

    private ParameterRoomType.RoomType getRoomType() {
        if (roomType != null) {
            return roomType;
        } else {
            IsoGameCharacter isoGameCharacter = this.getCharacter();
            if (isoGameCharacter == null) {
                return ParameterRoomType.RoomType.Generic;
            } else {
                BuildingDef buildingDef = isoGameCharacter.getCurrentBuildingDef();
                if (buildingDef == null) {
                    return ParameterRoomType.RoomType.Generic;
                } else {
                    IsoMetaGrid isoMetaGrid = IsoWorld.instance.getMetaGrid();
                    IsoMetaCell isoMetaCell = isoMetaGrid.getCellData(
                        PZMath.fastfloor(isoGameCharacter.x / 300.0F), PZMath.fastfloor(isoGameCharacter.y / 300.0F)
                    );
                    if (isoMetaCell != null && !isoMetaCell.roomTones.isEmpty()) {
                        RoomDef roomDef0 = isoGameCharacter.getCurrentRoomDef();
                        IsoMetaGrid.RoomTone roomTone0 = null;

                        for (int _int = 0; _int < isoMetaCell.roomTones.size(); _int++) {
                            IsoMetaGrid.RoomTone roomTone1 = (IsoMetaGrid.RoomTone)isoMetaCell.roomTones.get(_int);
                            RoomDef roomDef1 = isoMetaGrid.getRoomAt(roomTone1.x, roomTone1.y, roomTone1.z);
                            if (roomDef1 != null) {
                                if (roomDef1 == roomDef0) {
                                    return ParameterRoomType.RoomType.valueOf(roomTone1.enumValue);
                                }

                                if (roomTone1.entireBuilding && roomDef1.building == buildingDef) {
                                    roomTone0 = roomTone1;
                                }
                            }
                        }

                        return roomTone0 != null ? ParameterRoomType.RoomType.valueOf(roomTone0.enumValue) : ParameterRoomType.RoomType.Generic;
                    } else {
                        return ParameterRoomType.RoomType.Generic;
                    }
                }
            }
        }
    }

    private IsoGameCharacter getCharacter() {
        IsoPlayer isoPlayer0 = null;

        for (int _int = 0; _int < IsoPlayer.numPlayers; _int++) {
            IsoPlayer isoPlayer1 = IsoPlayer.players[_int];
            if (isoPlayer1 != null
                && (isoPlayer0 == null || isoPlayer0.isDead() && isoPlayer1.isAlive() || isoPlayer0.Traits.Deaf.isSet() && !isoPlayer1.Traits.Deaf.isSet())) {
                isoPlayer0 = isoPlayer1;
            }
        }

        return isoPlayer0;
    }

    public static void setRoomType(int _roomType) {
        try {
            roomType = ParameterRoomType.RoomType.values()[_roomType];
        } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
            roomType = null;
        }
    }

    public static void render(IsoPlayer player) {
        if (instance != null) {
            if (player == instance.getCharacter()) {
                player.drawDebugTextBelow("RoomType : " + instance.getRoomType().name());
            }
        }
    }

    private static enum RoomType {
        Generic(0),
        Barn(1),
        Mall(2),
        Warehouse(3),
        Prison(4),
        Church(5),
        Office(6),
        Factory(7);

        final int label;

        private RoomType(int int1) {
            this.label = int1;
        }
    }
}
